package itsm.isperp.framework.quartz.service;

import itsm.isperp.framework.domain.IBean;
import itsm.isperp.framework.quartz.domain.JobDefinition;
import itsm.isperp.framework.quartz.domain.JobState;
import itsm.isperp.framework.service.IBaseService;

import java.util.List;

import org.quartz.JobDetail;
import org.quartz.SchedulerException;
import org.quartz.impl.JobDetailImpl;

/**
 * 定时作业服务
 * 
 * @author lizx
 * @since 1.0
 */
public interface IJobService<T extends IBean> extends IBaseService<T> {

	public static final String BEAN_ID = "appJobService";

	public static final String JOB_DEFINITION_ID = "_job_definition_id_";

	/**
	 * 添加一个服务到调度器中
	 * 
	 * @param job
	 *            定时作业
	 * @throws SchedulerException
	 *             调度异常
	 */
	void addJobToScheduler(JobDefinition job) throws SchedulerException;

	/**
	 * 添加一个服务到调度器中
	 * 
	 * @param job
	 *            定时作业
	 * 
	 * @param jobDetail
	 *            定时作业的具体实现
	 * @throws SchedulerException
	 *             调度异常
	 */
	void addJobToScheduler(JobDefinition job, JobDetailImpl jobDetail)
			throws SchedulerException;

	/**
	 * 从调度器中
	 * 
	 * @param jobDefId
	 *            作业id
	 * @throws SchedulerException
	 */
	void removeJobFromScheduler(String jobDefId) throws SchedulerException;

	/**
	 * 从调度器中获取一个作业
	 * 
	 * @param jobDefId
	 *            作业id
	 * @return 作业详细{@link JobDetail}
	 * @throws SchedulerException
	 */
	JobDetail getJobFromScheduler(String jobDefId) throws SchedulerException;

	/**
	 * 恢复作业
	 * 
	 * @param jobDefId
	 *            作业id
	 * @throws SchedulerException
	 */
	void resumeJob(String jobDefId) throws SchedulerException;

	/**
	 * 中断作业
	 * 
	 * @param jobDefIds
	 *            作业id
	 * @throws SchedulerException
	 */
	void pauseJob(String jobDefIds) throws SchedulerException;

	/**
	 * 更新触发器
	 * 
	 * @param jobDefId
	 *            作业id
	 * @param cronExpression
	 *            定时表达式
	 * @throws SchedulerException
	 */
	void updateTrigger(String jobDefId, String cronExpression)
			throws SchedulerException;

	/**
	 * 载入指定状态的作业
	 * 
	 * @param state
	 * @return
	 */
	List<T> loadJobs(JobState state);

	/**
	 * 
	 * 
	 * @param state
	 * @return
	 */
	void updateJob(T jobDefinition);

	/**
	 * 更新作业状态
	 * 
	 * @param state
	 * @return
	 */
	void updateJobStatus(String id, JobState status);

}
